
import store from '@/store';
import roMap from "@/router/overAllviewRoleMap";
import token from './util.token';
import db from './util.db';
import { pathInit } from '@/store/modules/admin/modules/db';
import { cloneDeep } from 'lodash';

const welcome = "/welcome";

//获取子账号的默认主页
function getSubUserHomePage(){
    let page = "";
    let info = cloneDeep(db.get(pathInit({
        dbName: 'sys',
        path: 'user.info',
        user: true,
        defaultValue: {}
    })).value());
    let allRolesHomes = (info || {}).allRolesHomes || [];
    let groupCode = (store.state.admin.subUser.user || {}).groupCode;
    let customerType = (store.state.admin.subUser.user || {}).customerType;
    if(!groupCode && !customerType){
        return welcome;
    }
    let target = allRolesHomes.find(r => {return (r.groupCode && r.groupCode == groupCode) || (r.customerType && r.customerType == customerType)});
    if(target){
        page = target.functionUri || welcome;
    }else{
        page = welcome;
    }
    return page;
}

const homePage = {
    //获取首页
    getHomePage() {
        let page = "";
        let isSubUser = !store.getters["admin/subUser/isNull"];
        if(isSubUser){
            page =  getSubUserHomePage();
        }else{
            page = (store.state.admin.user.info || {}).home;
            if(!page){
                // let groupCode = (store.state.admin.user.info || {}).groupCode;
                // if(!groupCode){
                //     let tokenInfo = token.initToken() || {};
                //     groupCode = tokenInfo.groupCode;
                // }
                // page = roMap[groupCode];
                // if(!page) {
                //     page = welcome;
                // }
                page = welcome;
            }
        }
        return page;
    },
    
};

export default homePage;